package com.hmall.service;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.SysVisitorMapper;
import com.hmall.model.SysVisitor;
import com.hmall.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName VisitorService
 * @Description
 * @Author zhoutao
 * @Date 2018/11/27 14:31
 * @Version 2.0
 **/
@Service
public class VisitorService {


    private final SysVisitorMapper sysVisitorMapper;

    @Autowired
    public VisitorService(SysVisitorMapper sysVisitorMapper) {
        this.sysVisitorMapper = sysVisitorMapper;
    }

    public ServerResponse recordInformation(String nickName, String province, String city, String county, String phone) {

        SysVisitor visitor = new SysVisitor();
        visitor.setCity(city);
        visitor.setCounty(county);
        visitor.setCreateTime(new Date());
        visitor.setProvince(province);
        visitor.setNickName(nickName);
        visitor.setPhone(phone);
        sysVisitorMapper.insert(visitor);
        return ServerResponse.createBySuccess("添加访问信息成功");
    }

    public ServerResponse getVisitorInformation(Integer pageNum, Integer pageSize) {

        int start = pageSize * (pageNum - 1);
        int total = sysVisitorMapper.selectAll().size();
        int end = Math.min(start + pageSize, total);
        List<SysVisitor> sysVisitorList = sysVisitorMapper.selectByLimit(start, end);
        PageVo pageVo = new PageVo(pageNum, pageSize, total, sysVisitorList);
        return ServerResponse.createBySuccess(pageVo);
    }
}
